package com.module.base.widget.picker;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.module.base.utils.TimeUtils;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.module.base.R;

import java.util.Calendar;
import java.util.Date;

/**
 * @author yanxuwen
 * @date 2018/7/19 16:03
 * @desc
 */
public class CommonTimePicker {


    public static int TYPE_FULL_DATE = 0x11;
    public static int TYPE_DATE = 0x12;
    public static int TYPE_MONTH = 0x13;

    public TimePickerView timePickerView;

    public int currentType;
    public ITimeSelectListener iSelectTimeCallback;
    public Context context;

    public interface ITimeSelectListener {
        void onChange(Date date);

        void onSelect(Date date, int type);
    }

    int tag = -1;

    public CommonTimePicker(@NonNull Context context) {
        this.context = context;
    }

    public boolean[] getPickerTime() {

        if (currentType == TYPE_DATE) {
            return new boolean[]{true, true, true, false, false, false};
        } else if (currentType == TYPE_MONTH) {
            return new boolean[]{true, true, false, false, false, false};
        } else {
            return new boolean[]{true, true, true, true, true, true};

        }

    }

    public CommonTimePicker setOnSelectListener(@NonNull ITimeSelectListener onSelectListener) {
        this.iSelectTimeCallback = onSelectListener;
        return this;
    }

    public void show() throws Exception {
        if (timePickerView == null) {
            initPicker(context);

            if (iSelectTimeCallback == null) {
                throw new Exception(" iSelectTimeCallback is null");
            }
        }
        if (timePickerView != null) {
            timePickerView.show();
        }
    }

    public void show(int tag) throws Exception {
        this.tag = tag;
        if (timePickerView == null) {
            initPicker(context);

            if (iSelectTimeCallback == null) {
                throw new Exception(" iSelectTimeCallback is null");
            }
        }
        if (timePickerView != null) {
            timePickerView.show();
        }
    }


    //默认20年
    public CommonTimePicker init(int type) {
        this.currentType = type;
        initPicker(context);
        return this;
    }

    public CommonTimePicker initDefault(int type) {
        this.currentType = type;
        initPickerDefault(context);
        return this;
    }

    /**
     * 缓存上一次的选择
     */
    public Date lastDate;

    public String toType() {
        if (currentType == TYPE_DATE) {
            return TimeUtils.date2Str(lastDate);
        } else if (currentType == TYPE_MONTH) {
            return TimeUtils.date2MouthStr(lastDate);
        } else {
            return TimeUtils.date2FullStr(lastDate);
        }
    }


    private CommonTimePicker initPicker(Context context) {
        return initPicker(context, 20);
    }

    private CommonTimePicker initPickerDefault(Context context) {
        return initPicker(context, 0);
    }

    int lastYears;

    private CommonTimePicker initPicker(Context context, int lastYears) {
        Calendar startDate, endDate;
        this.lastYears = lastYears;
        TimePickerBuilder timePickerViewBuild = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                lastDate = date;
                if (iSelectTimeCallback != null) {
                    iSelectTimeCallback.onSelect(date, tag);
                }
            }
        }).setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
            @Override
            public void onTimeSelectChanged(Date date) {
                if (iSelectTimeCallback != null) {
                    iSelectTimeCallback.onChange(date);
                }
            }
        });

        if (lastYears > 0) {
            startDate = Calendar.getInstance();
            startDate.set(Calendar.YEAR, startDate.get(Calendar.YEAR) - lastYears);
            endDate = Calendar.getInstance();
            timePickerViewBuild.setRangDate(startDate, endDate);
            timePickerViewBuild.setDate(endDate);
        } else {

        }
        timePickerViewBuild.setType(getPickerTime());
        timePickerView = timePickerViewBuild.isDialog(true)
                .setSubmitColor(context.getResources().getColor(R.color.color_picker))
                .setCancelColor(context.getResources().getColor(R.color.color_picker))
                .build();


        Dialog mDialog = timePickerView.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            timePickerView.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }
        return this;
    }


}
